package com.qf.dao.impl;

import com.qf.dao.ProductDao;
import com.qf.pojo.Product;
import com.qf.utils.DruidJdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 高圆圆
 * @date 2023/1/3 20:07
 * 商品的数据访问接口实现
 */
public class ProductDaoImpl  implements ProductDao {
    /**
     * 查询所有的最新商品
     * @return 返回商品列表
     */
    @Override
    public List<Product> selectNewProducts() throws SQLException {

        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from product order by pdate asc limit 9" ;
        //查询
        List<Product> list = qr.query(sql, new BeanListHandler<>(Product.class));
        return list;
    }

    /**
     * 查询热门商品
     * @return 返回热门商品列表
     */
    @Override
    public List<Product> selectHotProducts() throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "SELECT * FROM  product WHERE is_hot = 1 ORDER BY pdate DESC LIMIT 9  ;" ;
        //查询
        List<Product> list = qr.query(sql, new BeanListHandler<>(Product.class));
        return list;
    }

    /**
     * 数据访问接口通过商品pid查询指定商品
     * @param pid 商品编号
     * @return  返回商品实体
     */
    @Override
    public Product selectProductByPid(String pid) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = "select * from product where pid = ?" ;
        //执行查询
        Product product = qr.query(sql, new BeanHandler<Product>(Product.class), pid);
        return product;
    }

    /**
     * 商品的数据访问接口获取商品分页列表数据
     * @param cid 分类id
     * @param currentPage 当前页码
     * @param pageSize    每页显示的条数
     * @return 返回商品分页数据
     */
    @Override
    public List<Product> selectProductByPage(String cid, int currentPage, int pageSize) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql = " SELECT * FROM product WHERE cid =  ? LIMIT ?,?;" ;
        //执行
        List<Product> products = qr.query(sql, new BeanListHandler<Product>(Product.class),
                cid,
                (currentPage - 1) * pageSize,
                pageSize);
        return products;
    }

    /**
     * 查询指定商品分类的总记录数
     * @param cid 商品的分类id
     * @return 返回商品的总记录数
     */
    @Override
    public int selectTotalCount(String cid) throws SQLException {
        //执行对象
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        //sql
        String sql  = "SELECT COUNT(pid) FROM product WHERE cid = ? ;" ;
        //查询
        Object obj = qr.query(sql, new ScalarHandler<>(), cid);
        int totalCount = Integer.parseInt(String.valueOf(obj)) ;
        return totalCount;
    }

    @Override
    public List<Product> selectProducts() throws SQLException {
        QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource()) ;
        String sql = "select * from product" ;
        List<Product> products = qr.query(sql, new BeanListHandler<Product>(Product.class));
        return products;
    }
}
